package bme.andraskindler.treenventory.provider;

import java.util.List;

import android.graphics.Color;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import bme.andraskindler.treenventory.R;
import bme.andraskindler.treenventory.activity.TreeTypeActivity;
import bme.andraskindler.treenventory.model.TypeData;

/**
 * Szabványos listaadapter, a {@link TreeTypeActivity} használja.
 * @author andraskindler
 *
 */
public class TypeListAdapter extends BaseAdapter {

	private List<TypeData> mList;
	private boolean mSortHungarian = true;

	public TypeListAdapter() {
		mList = TreenventoryDataSource.getAllTypes(mSortHungarian);
	}

	public int getCount() {
		return mList.size();
	}

	public TypeData getItem(int position) {
		return mList.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		TextView listItem = (TextView) convertView;

		if (listItem == null) {
			listItem = (TextView) LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_treetype, null);
		}

		final TypeData type = mList.get(position);

		String htmlString = type.getHun() + "<br/><small>" + type.getLatin() + "</small>";
		listItem.setText(Html.fromHtml(htmlString));
		listItem.setBackgroundColor(Color.LTGRAY);

		return listItem;
	}

	@Override
	public void notifyDataSetChanged() {
		mList = TreenventoryDataSource.getAllTypes(mSortHungarian);
		super.notifyDataSetChanged();
	}

	public boolean isSortHungarian() {
		return mSortHungarian;
	}

	public void toggleSortHungarian() {
		mSortHungarian = !mSortHungarian;
		notifyDataSetChanged();
	}

	public void search(final String s) {
		mList = TreenventoryDataSource.getTypeForName(s, mSortHungarian);
		super.notifyDataSetChanged();
	}
}
